package com.qfedu.service.impl;

import com.qfedu.constants.PageEnum;
import com.qfedu.dao.MilkTeaMenuDao;
import com.qfedu.dao.impl.MilkTeaMeauDaoImpl;
import com.qfedu.model.MilkTea;
import com.qfedu.page.PageBean;
import com.qfedu.service.MilkTeaMeauService;

import java.awt.print.Book;
import java.util.List;

public class MilkTeaMeauServiceImpl implements MilkTeaMeauService {
    PageBean<MilkTea> pageBean=new PageBean<>();
    MilkTeaMenuDao mtmDao=new MilkTeaMeauDaoImpl();

    @Override
    public PageBean<MilkTea> findPage(int pageNo, int pageSize) {
        PageBean<MilkTea> pageBean = new PageBean<>();

        if (pageNo<=0){ //校验当前页
            pageNo=1;
        }
        pageBean.setPageNo(pageNo);
        if (pageSize<=0){
            pageSize=PageEnum.DEFAULTSIZE.getSize(); //枚举类名称.枚举对象.get方法
        }
        pageBean.setPageSize(pageSize);
        //查询所有记录数
        int count = mtmDao.count();
        pageBean.setPageTotalCount(count);
        //求出总页数
        int counts=count%pageSize;
        //定义总页数
        int pageTotal = 0;
       if (counts>0){
           pageTotal=count/pageSize+1;
       }
       if (counts==0){
           pageTotal=count/pageSize;
       }
        pageBean.setPageTotal(pageTotal);
        //查询当前页的数据
        List<MilkTea> milkTea = mtmDao.findMilkTea(pageNo, pageSize);
        pageBean.setItems(milkTea);
        return pageBean;
    }

    @Override
    public MilkTea findMilkTeaById(int byId) {
        return mtmDao.findMilkTeaById(byId);
    }
    //添加
    @Override
    public int add(MilkTea milkTea) {
        return mtmDao.add(milkTea);
    }
    //删除
    @Override
    public int delete(int tid) {
        return mtmDao.delete(tid);
    }
    //修改
    @Override
    public int update(MilkTea milkTea) {
        return mtmDao.update(milkTea);
    }
    //查询所有
    @Override
    public List<MilkTea> findAll() {
        return mtmDao.findMilkTeaAll();
    }
}
